﻿using System.Collections.Generic;
using UnityEngine;

namespace Yoozoo.Modules.Furnitures
{
    public class Furniture : MonoBehaviour
    {
        [Tooltip("X向格子数(0.5m)")]
        [Range(1, 20)]
        public int X = 1;
        [Tooltip("Y向格子数(0.5m)")]
        [Range(1, 20)]
        public int Y = 1;
        [Tooltip("轴向位置(0~1)")]
        public Vector2 Pivot = new Vector2(0.5f, 0.5f);
        [Tooltip("旋转角度")]
        [Range(0, 360)]
        public float Rotate;
        [Tooltip("轴心")]
        public Transform PivotTransform;
        [Tooltip("交互点")]
        public List<Vector2> InteractiveLocations = new List<Vector2>();
        [Tooltip("交互点预览数量")]
        public int InteractiveLocationViewCount = 0;
        [Tooltip("交互点最少数量")]
        public int InteractiveLocationMinCount = 0;

        /// <summary>
        /// X轴方向
        /// </summary>
        public Vector3 AxisX
        {
            get
            {
                if (PivotTransform)
                {
                    return PivotTransform.right;
                }
                return Vector3.right;
            }
        }

        /// <summary>
        /// Y轴方向
        /// </summary>
        public Vector3 AxisY
        {
            get
            {
                if (PivotTransform)
                {
                    return PivotTransform.forward;
                }
                return Vector3.forward;
            }
        }

        /// <summary>
        /// 轴心坐标
        /// </summary>
        public Vector3 PivotCoordinate
        {
            get
            {
                if (PivotTransform)
                {
                    return PivotTransform.position;
                }
                return Vector3.zero;
            }
        }

        /// <summary>
        /// 根据轴心和偏移获取点的位置
        /// </summary>
        /// <param name="pivot"></param>
        /// <param name="offsetX">x轴向偏移（单位0.5）</param>
        /// <param name="offsetY">y轴向偏移（单位0.5）</param>
        /// <returns></returns>
        Vector3 GetPoint(Vector3 pivot, float offsetX, float offsetY)
        {
            var point = pivot;
            point += offsetX * AxisX * 0.5f;
            point += offsetY * AxisY * 0.5f;
            return point;
        }

        void OnValidate()
        {
            if (PivotTransform == null)
            {
                PivotTransform = transform;
            }
        }

        public bool Contains(Vector2 location)
        {
            foreach (var interactiveLocation in InteractiveLocations)
                if (interactiveLocation == location)
                    return true;

            return false;
        }

        public void RemoveLocation(Vector2 location)
        {
            for (var i = 0; i < InteractiveLocations.Count; i++)
            {
                var interactiveLocation = InteractiveLocations[i];
                if (interactiveLocation == location)
                {
                    InteractiveLocations.RemoveAt(i);
                    break;
                }
            }
        }

        public void AddLocation(Vector2 location)
        {
            if (Contains(location))
                return;
            
            InteractiveLocations.Add(location);
        }


        void OnDrawGizmos()
        {
            //           ↑ y轴
            //           ↑
            //   (0, 1)  ↑    (1, 1)
            //    p3 --------- p2
            //    |      p     |  →→→ x轴
            //    p0 --------- p1
            //   (0, 0)       (1, 0)
            //   pivot = (0.5, 0.5)


            var color = Gizmos.color;
            Gizmos.color = Color.yellow;

            var p0 = GetPoint(PivotCoordinate, Pivot.x * -X, Pivot.y * -Y);
            var p1 = GetPoint(PivotCoordinate, (1 - Pivot.x) * X, Pivot.y * -Y);
            var p2 = GetPoint(PivotCoordinate, (1 - Pivot.x) * X, (1 - Pivot.y) * Y);
            var p3 = GetPoint(PivotCoordinate, Pivot.x * -X, (1 - Pivot.y) * Y);

            Gizmos.DrawLine(p0, p1);
            Gizmos.DrawLine(p1, p2);
            Gizmos.DrawLine(p2, p3);
            Gizmos.DrawLine(p3, p0);

            Gizmos.DrawSphere(p0, 0.1f);
            Gizmos.DrawSphere(p1, 0.1f);
            Gizmos.DrawSphere(p2, 0.1f);
            Gizmos.DrawSphere(p3, 0.1f);

            // 交互点
            //         (0, Y)     (X-1, Y)
            // (-1, Y-1) p3 --------- p2 (X, Y-1)
            //           |            |
            // (-1, 0)   p0 --------- p1 (X, 0)
            //         (0, -1)       (X-1, -1)

            Gizmos.color = Color.yellow;
            var size = new Vector3(0.48f, 0.1f, 0.48f);
            for (int i = 0; i < X; i++)
            {
                // p3 -> p2 的上方
                var center = GetPoint(PivotCoordinate, Pivot.x * -X + 0.5f * (1 + 2 *i), (1 - Pivot.y) * Y + 0.5f);
                Gizmos.DrawCube(center, size);

                // p0 -> p1 的下方
                center = GetPoint(PivotCoordinate, Pivot.x * -X + 0.5f * (1 + 2 * i), Pivot.y * -Y - 0.5f);
                Gizmos.DrawCube(center, size);
            }

            for (int i = 0; i < Y; i++)
            {
                // p0 -> p3 的左侧
                var center = GetPoint(PivotCoordinate, Pivot.x * -X - 0.5f, Pivot.y * -Y + 0.5f * (1 + 2 * i));
                Gizmos.DrawCube(center, size);

                // p1 -> p2 的右侧
                center = GetPoint(PivotCoordinate, (1 - Pivot.x) * X + 0.5f, Pivot.y * -Y + 0.5f * (1 + 2 * i));
                Gizmos.DrawCube(center, size);
            }

            Gizmos.color = Color.green;
            foreach (var interactiveLocation in InteractiveLocations)
            {
                var center = GetPoint(PivotCoordinate, 
                    Pivot.x * -X + 0.5f * (1 + 2 * interactiveLocation.x), 
                    Pivot.y * -Y + 0.5f * (1 + 2 * interactiveLocation.y));
                Gizmos.DrawCube(center, size);
            }

            Gizmos.color = color;
        }
    }
}
